﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

[XLua.LuaCallCSharp]
public static class UGUI_Extensions
{
    public static void AddEventCallback(this GameObject ui, EventTriggerType ett, System.Action<BaseEventData> cb)
    {
        var trigger = ui.transform.gameObject.GetComponent<EventTrigger>();
        if (trigger == null)
        {
            trigger = ui.transform.gameObject.AddComponent<EventTrigger>();
        }

        if(trigger.triggers == null)
        {
            trigger.triggers = new List<EventTrigger.Entry>();
        }
 
        // List<EventTrigger.Entry> ls = trigger.triggers;

        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = ett;
        entry.callback = new EventTrigger.TriggerEvent();
        UnityEngine.Events.UnityAction<BaseEventData> callback = new UnityEngine.Events.UnityAction<BaseEventData>(cb);
        entry.callback.AddListener(callback);

        trigger.triggers.Add(entry);
    }

}
